package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "套餐管理")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;


    /**
     *  套餐启售停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐启售停售")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result stratOrStop(@PathVariable Integer status,Long id){
        log.info("前后端连通");
        this.setmealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     *  修改套餐信息
     */
    @PutMapping
    @ApiOperation("套餐修改")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("前后端连通");
        // 调用service方法修改套餐信息
        this.setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     *  套餐信息回显
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id查询套餐信息")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("前后端连通");
        SetmealVO setmealVO = this.setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    /**
     *  套餐批量删除
     */
    @DeleteMapping
    @ApiOperation("套餐批量删除")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("前后端连通");
        // 调用service批量删除套餐
        this.setmealService.deleteByIds(ids);
        return Result.success();
    }

    /**
     *  套餐分页查询
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("前后端连通");
        // 调用service方法获取数据
        PageResult pageResult = this.setmealService.pageQuery(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     *  新增套餐
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache" , allEntries = true)
    public Result saveSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("前后端连通");

        this.setmealService.save(setmealDTO);

        return Result.success();
    }
}
